package com.tykboot.controller;

import com.tykboot.core.base.BaseController;
import com.tykboot.core.persistence.Criteria;
import com.tykboot.core.persistence.Restrictions;
import com.tykboot.entity.TbAccountRequest;
import com.tykboot.message.ResponseMsg;
import com.tykboot.service.ITbAccountRequestService;
import com.tykboot.vo.AccountRequestVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  Controller
 *
 * @author LiuYu
 */ 
@Api(tags = "account-request", description = "/api/account-requests")
@RestController
@RequestMapping(BaseController.API + "/account-requests")
public class TbAccountRequestController extends BaseController {

	@Autowired
	private ITbAccountRequestService tbAccountRequestService;

	@ApiOperation(value = "Add an account", notes = "Add an account")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true)
	})
	@PostMapping("")
	public ResponseMsg<TbAccountRequest> add(@RequestHeader("financialId") String financialId,
											 @RequestBody AccountRequestVo requestBody) {
		ResponseMsg<TbAccountRequest> responseMsg = new ResponseMsg<>();
		TbAccountRequest entity = new TbAccountRequest();
		BeanUtils.copyProperties(requestBody, entity);
		entity.setFinancialId(financialId);
		entity.setPermissionsDb(StringUtils.join(requestBody.getPermissions(), ","));
		TbAccountRequest result = tbAccountRequestService.save(entity);
		responseMsg.setData(result);
		return responseMsg;
	}

	@ApiOperation(value = "Delete an account request", notes = "Delete an account request")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true),
			@ApiImplicitParam(name = "accountRequestId", value = "accountRequestId", dataType = "string", paramType = "path", required = true)
	})
	@DeleteMapping("/{accountRequestId}")
	public ResponseMsg deleteById(@RequestHeader("financialId") String financialId,
								  @PathVariable("accountRequestId") String accountRequestId) {
		ResponseMsg responseMsg = new ResponseMsg();
		tbAccountRequestService.deleteById(accountRequestId);
		return responseMsg;
	}

	@ApiOperation(value = "Get an account", notes = "Get an account")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true),
			@ApiImplicitParam(name = "accountRequestId", value = "accountRequestId", dataType = "string", paramType = "path", required = true)
	})
	@GetMapping("/{accountRequestId}")
	public ResponseMsg<TbAccountRequest> queryById(@RequestHeader("financialId") String financialId,
												   @PathVariable("accountRequestId") String accountRequestId) {
		ResponseMsg<TbAccountRequest> responseMsg = new ResponseMsg<>();
		Criteria<TbAccountRequest> req = new Criteria<>();
		req.add(Restrictions.eq("accountRequestId", accountRequestId));
		req.add(Restrictions.eq("financialId", financialId));
		List<TbAccountRequest> accountRequests = tbAccountRequestService.findAll(req);
		if (accountRequests != null && !accountRequests.isEmpty()) {
			responseMsg.setData(accountRequests.get(0));
		}
		return responseMsg;
	}

	@ApiOperation(value = "Get all account", notes = "Get an account")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true)
	})
	@GetMapping("")
	public ResponseMsg<List<TbAccountRequest>> queryAll(@RequestHeader("financialId") String financialId) {
		ResponseMsg<List<TbAccountRequest>> responseMsg = new ResponseMsg<>();
		Criteria<TbAccountRequest> req = new Criteria<>();
		req.add(Restrictions.eq("financialId", financialId));
		responseMsg.setData(tbAccountRequestService.findAll(req));
		return responseMsg;
	}

}
